package com.jilian.mydemo.modul.my.ui;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.jilian.mydemo.MyApplication;
import com.jilian.mydemo.R;
import com.jilian.mydemo.base.BaseActivity;
import com.jilian.mydemo.base.BaseDto;
import com.jilian.mydemo.common.dto.AccountDto;
import com.jilian.mydemo.common.dto.WalletDto;
import com.jilian.mydemo.listener.CustomItemClickListener;
import com.jilian.mydemo.modul.my.adapter.WalletAdapter;
import com.jilian.mydemo.modul.order.viewmodel.OrderViewModel;
import com.jilian.mydemo.utils.EmptyUtils;
import com.jilian.mydemo.utils.ToastUitl;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tencent.mm.opensdk.modelmsg.SendAuth;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class MyWalletActivity extends BaseActivity implements CustomItemClickListener {

    private RecyclerView recyclerview;
    private WalletAdapter adapter;
    private List<WalletDto> datas;
    private LinearLayoutManager linearLayoutManager;
    private TextView tvBindWchat;
    private TextView tvWithdrawal;
    private OrderViewModel orderViewModel;
    private TextView tvCount;
    private SmartRefreshLayout srNoData;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.removeActivity(this);
    }

    @Override
    protected void createViewModel() {
        orderViewModel = ViewModelProviders.of(this).get(OrderViewModel.class);
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_mywallet;
    }

    @Override
    public void initView() {
        setNormalTitle("钱包", v -> finish());
        tvBindWchat = (TextView) findViewById(R.id.tv_bind_wchat);
        tvWithdrawal = (TextView) findViewById(R.id.tv_withdrawal);
        recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
        srNoData = (SmartRefreshLayout) findViewById(R.id.sr_no_data);
        srNoData.setEnableLoadMore(false);
        tvCount = (TextView) findViewById(R.id.tv_count);
        linearLayoutManager = new LinearLayoutManager(this);
        datas = new ArrayList<>();
        recyclerview.setLayoutManager(linearLayoutManager);
        adapter = new WalletAdapter(this, datas, this);
        recyclerview.setAdapter(adapter);
    }

    @Override
    public void initData() {
        showLoadingDialog();
        getWallete();
    }

    private void getWallete() {
        orderViewModel.account();
        orderViewModel.getAccountliveData().observe(this, new Observer<BaseDto<AccountDto>>() {
            @Override
            public void onChanged(@Nullable BaseDto<AccountDto> accountDtoBaseDto) {
                hideLoadingDialog();
                if (accountDtoBaseDto.isSuccess()) {
                    if (EmptyUtils.isNotEmpty(accountDtoBaseDto.getData())) {
                        tvCount.setText(accountDtoBaseDto.getData().getAmount() == null ? "0.00" : accountDtoBaseDto.getData().getAmount().setScale(2, BigDecimal.ROUND_DOWN).toString());

                        if (EmptyUtils.isNotEmpty(accountDtoBaseDto.getData().getLists())) {
                            datas.clear();
                            datas.addAll(accountDtoBaseDto.getData().getLists());
                            adapter.notifyDataSetChanged();
                            srNoData.setVisibility(View.GONE);
                            recyclerview.setVisibility(View.VISIBLE);
                        } else {
                            srNoData.setVisibility(View.VISIBLE);
                            recyclerview.setVisibility(View.GONE);
                        }
                    }


                } else {
                    ToastUitl.showImageToastTips(accountDtoBaseDto.getMsg());
                }
            }
        });
    }

    @Override
    public void initListener() {
        srNoData.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getWallete();
            }
        });
        tvBindWchat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SendAuth.Req req = new SendAuth.Req();
                req.scope = "snsapi_userinfo";
                req.state = "wechat_sdk_bind";
                MyApplication.getInstance().getApi().sendReq(req);
            }
        });
        tvWithdrawal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MyWalletActivity.this, WithdrawalActivity.class));
            }
        });
    }

    @Override
    public void onItemClick(View view, int position) {

    }
}
